import { gameConfigHandle } from "js/config/gameConfig"
import Random from "js/core/random"
import utils from "js/core/utils"
import { EffectModel, FighterAttriModel, FighterModel } from "js/model/fighterModel"
import { HurtType, MapType, MonsterType } from "js/typeDefine/typeDefine"
import fighterHandle from "./fighterHandle"
import { SkillModel } from "js/model/fighterModel"

class MonsterHandle {
    createMonster(mapID: number, monsterLvl: number, monsterType: MonsterType, random) {
        let mapConfig = gameConfigHandle.configByID[mapID]
        let monsterID: number = utils.getOneFromArray(mapConfig.monsters, random)
        let fighter = new FighterModel()
        fighter.type = monsterType
        fighter.attri = new FighterAttriModel()
        fighter.tID = monsterID
        fighter.lvl = monsterLvl
        let monsterConfig = gameConfigHandle.gameConfig.monster
        let monsterItemConfig = gameConfigHandle.configByID[monsterID]
        fighter.attri.hurtType = HurtType.Scrap
        let monsterTypeCoe = monsterConfig.monsterTypeCoeOfLowLvl[monsterType]
        let lowLvl = monsterConfig.lowLvl
        if (monsterLvl > lowLvl) {
            monsterTypeCoe = monsterConfig.monsterTypeCoe[monsterType]
        }

        // //属性倍率计算
        let beginLvl = gameConfigHandle.gameConfig.monster.beginLvl
        let multipleConfig = gameConfigHandle.gameConfig.monster.multiple
        let multipleMax = gameConfigHandle.gameConfig.monster.multipleMax
        let multiple = 1
        if (monsterLvl > beginLvl) {
            let mLvl = Math.floor((monsterLvl - beginLvl) / 10)
            multiple = utils.getIncValue(multipleConfig, mLvl, true)
            if (multiple > multipleMax) {
                multiple = multipleMax
            }
        }
        // 属性成长缩减
        let rand = new Random(monsterID)
        let attriList = mapConfig.mapType == MapType.Fantasy ? monsterConfig.fantasyAttri : monsterConfig.baseAttri
        Object.entries(attriList).forEach(attris => {
            let attriName = attris[0]
            let attriValue: any = attris[1]
            let value = utils.getIncValue(attriValue, monsterLvl)
            if (attriName == 'maxHp') {
                value = value * monsterTypeCoe.maxHp
            } else {
                value = value * monsterTypeCoe.other
            }
            if (attriName == 'maxHp' || attriName == 'attack' || attriName == 'def') {
                value *= multiple
            }
            if (monsterItemConfig.baseAttri[attriName]) {
                if (attriName == "attackSpeed") {
                    fighter.attri[attriName] = monsterItemConfig.baseAttri[attriName] || 150
                } else {
                    value = value * (1 + monsterItemConfig.baseAttri[attriName] / 100)
                    fighter.attri[attriName] += value
                }
            } else {
                fighter.attri[attriName] += value
            }
            if (attriName == "hit" || attriName == "dodge") {
              
                let per = rand.intn(100) - 50
                fighter.attri[attriName] = fighter.attri[attriName] * (100 - per) / 100
            }
        })
        fighter.attri.hurtType = monsterItemConfig.hurtType || HurtType.Normal
        fighter.hatredType = gameConfigHandle.configByID[fighter.tID].hatredType || 1
        fighter.skills = []
        if ((mapID == 100022 && monsterLvl > 900) || mapConfig.mapType == MapType.Fantasy) {
            this.getDefaultSkill(fighter)
            this.calcSkill(fighter)
            if (rand.intn(100) < 10) {
                fighter.hatredType = 2
            }
        }
        fighterHandle.calcBattleFighterAttri(fighter)
        fighter.battleAttri.hp = fighter.battleAttri.maxHp
        let bossMapList = gameConfigHandle.gameConfig.base.bossMapList
        if(bossMapList.includes(mapID)){
            fighter.attri.reduceHurt = 50
        }
        return fighter
    }

    getDefaultSkill(fighter: FighterModel) {
        let monsterConfig = gameConfigHandle.configByID[fighter.tID]
        let skills = monsterConfig.skills || [130301]
        skills.map((sid, i) => {
            let skill = new SkillModel()
            skill.skillID = sid
            skill.skillLvl = 1
            skill.auxiliarySkills = []
            let idx = fighter.skills.findIndex(s => s.skillID == sid)
            if (idx == -1) {
                fighter.skills.push(skill)
            }
        })
    }

    getSkillLvl(skillLvl: number) {
        let jjLvl = Math.floor((skillLvl - 1) / 10) + 1
        return jjLvl
    }

    calcSkill(fighter: FighterModel) {
        let skills = fighter.skills
        for (let i = 0; i < skills.length; i++) {
            let skill = skills[i]
            let skillID = skill.skillID
            let skillLvl = skill.skillLvl || 1
            let skillConfig = gameConfigHandle.configByID[skillID]
            if (Array.isArray(skillConfig.cd)) {
                let jjLvl = this.getSkillLvl(skill.skillLvl)
                skill.cd = utils.getIncValue(skillConfig.cd, jjLvl)
            } else {
                skill.cd = skillConfig.cd
            }
            if (skillConfig.triggers) {
                skillConfig.triggers.forEach(triggerItem => {
                    if (skillLvl >= triggerItem.limitLvl || !triggerItem.limitLvl) {
                        let trigger = new EffectModel({
                            skillID: skillID,
                            type: triggerItem.triggerType,
                            skillLvl: skillLvl,
                            data: JSON.parse(JSON.stringify(triggerItem))
                        })
                        if (!fighter.effects[triggerItem.triggerType]) {
                            fighter.effects[triggerItem.triggerType] = []
                        }
                        fighter.effects[triggerItem.triggerType].push(trigger)
                    }
                })
            }
            let cd = skill.cd
            skill.cd = utils.floor(cd * (100 - skill.cdPer) / 100)
            //出招属性降低技能CD
            let reduceSkillCd = fighter.attri.reduceSkillCd
            // skill.cd -= reduceSkillCd * 50
            let skillsReduceCd = fighter.skillsReduceCd
            if (skillsReduceCd[skillID]) {
                skill.cd -= skillsReduceCd[skillID] * 1000
            }
        }
    }

    calcMonsterCoe (mapID, floor) {
        let coe = 50
        let mapConfig = gameConfigHandle.configByID[mapID]
        if (mapConfig.mapType == MapType.Normal) {
            let lvl = mapConfig.mapLvl
            let addCoe = utils.getIncValue([0, 0.5], lvl)
            if (addCoe > 50) {
                addCoe = 50
            }
            coe += addCoe
        } else if (mapConfig.mapType == MapType.Fantasy) {
            coe = 40
            let lvl = mapConfig.mapLvl - 100
            let addCoe = utils.getIncValue([0, 2], lvl)
            if (addCoe > 60) {
                addCoe = 60
            }
            coe += addCoe
        }
        return coe
    }
}


let monsterHandle = new MonsterHandle()

export default monsterHandle